﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class YiGongSig : System.Web.UI.PageEx
{
    private string strResult = string.Empty;
    private DataSet ds = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            DataBinds();
        }
    }
    private void DataBinds()
    {
        ds = DalEx.DbCompInfoEx.Select(" and cStatus=1");
        rptType.DataSource = ds;
        rptType.DataBind();

        Model.DbCompInfo dbCompInfo = DalEx.DbCompInfoEx.GetModel("YiGongBaoMing");
        if (dbCompInfo != null && (dbCompInfo.CEndDate.CompareTo(DateTime.Now) >= 0 || dbCompInfo.CEndDate.CompareTo(SqlHelper.MinDate) == 0))
        {
            setMeta("家长义工报名表", dbCompInfo.CKeywords, dbCompInfo.CDescription);
        }
        else
        {
            MessageBox("抱歉，当前报名信息不存在或已结束报名！", "Default.aspx");
            return;
        }

        #region 如会员已登录则绑定数据
        if (DalEx.DbUserDalEx.CheckUserLogin(UserType, UserId, ref strResult))
        {
            pnlLoginInfo.Enabled = false;
            pnlLoginInfo.Visible = false;

            Model.DbUsers dbUsers = DalEx.DbUserDalEx.Select(UserId);
            if (dbUsers != null)
            {
                txtParentName.Text = dbUsers.FirstName + dbUsers.LastName;
                txtMobile.Text = dbUsers.Tel;
                hfUserId.Value = dbUsers.UserID.ToString();
            }
        }
        #endregion
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        Model.DbCompInfo dbCompInfo = DalEx.DbCompInfoEx.GetModel("YiGongBaoMing");
        if (dbCompInfo == null || (dbCompInfo.CEndDate.CompareTo(DateTime.Now) < 0 && dbCompInfo.CEndDate.CompareTo(SqlHelper.MinDate) != 0))
        {
            MessageBox("抱歉，当前报名信息不存在或已结束报名！", "Default.aspx");
            return;
        }
        string strOrderSN = WebBase.Comm.GetOrderNo("ZC");
        string strBabyName = SqlHelper.String(txtBabyName.Text);
        string strParentName = SqlHelper.String(txtParentName.Text);
        string strParentEducation = SqlHelper.String(ddlParentEducation.SelectedValue);
        string strMobile = SqlHelper.String(txtMobile.Text);
        string strWorkUnit = SqlHelper.String(txtWorkUnit.Text);
        string strParentSkill = SqlHelper.String(txtParentSkill.Text);
        string strResources = SqlHelper.String(txtResources.Text);
        if (strBabyName.Length > 50)
        {
            MessageBox("幼儿姓名不能超过50个字！");
            return;
        }
        if (strParentName.Length > 100)
        {
            MessageBox("幼儿姓名不能超过100个字！");
            return;
        }
        if (strParentEducation == "")
        {
            MessageBox("请选择家长学历！");
            return;
        }
        if (strMobile.Length > 50)
        {
            MessageBox("联系电话不能超过50个字！");
            return;
        }
        if (strWorkUnit.Length > 200)
        {
            MessageBox("工作单位不能超过200个字！");
            return;
        }
        if (strParentSkill.Length > 250)
        {
            MessageBox("家长特长不能超过250个字！");
            return;
        }
        if (strResources.Length > 500)
        {
            MessageBox("可提供的资源不能超过500个字！");
            return;
        }


        int iUserId = -1;
        int.TryParse(hfUserId.Value, out iUserId);
        iUserId = iUserId == 0 ? -1 : iUserId;

        string strLoginName = SqlHelper.String(txtLoginUser.Text);
        string strEmail = SqlHelper.String(txtEmail.Text);
        string strLoginPwd = SqlHelper.String(txtLoginPwd.Text);

        if (iUserId < 1)
        {
            Model.DbUsers dbUsers = DalEx.DbUserDalEx.GetModel(" and UserName='" + strLoginName + "' and UserPwd='" + SqlHelper.MD5(strLoginPwd, 32) + "'");
            if (dbUsers != null)
            {
                iUserId = dbUsers.UserID;
                strLoginName = dbUsers.UserName;
            }
            else
            {
                dbUsers = new Model.DbUsers();
                dbUsers.UserName = strLoginName;
                dbUsers.Email = strEmail;
                dbUsers.UserPwd = strLoginPwd;
                dbUsers.Sex = 1;
                dbUsers.AddTime = DateTime.Now;
                dbUsers.LastLoginTime = SqlHelper.MinDate;
                dbUsers.TotalCost = 0;
                dbUsers.LoginTimes = 0;
                dbUsers.Status = 1;
                dbUsers.LastIP = "127.0.0.1";
                if (DalEx.DbUserDalEx.Insert(dbUsers, ref strResult, ref iUserId) < 1)
                {
                    MessageBox(strResult);
                    return;
                }
            }
        }

        Model.BaoMing2 M_BaoMing2 = new Model.BaoMing2();
        M_BaoMing2.OrderNO = strOrderSN;
        M_BaoMing2.BabyName = strBabyName;
        M_BaoMing2.ParentName = strParentName;
        M_BaoMing2.ParentEducation = strParentEducation;
        M_BaoMing2.Mobile = strMobile;
        M_BaoMing2.WorkUnit = strWorkUnit;
        M_BaoMing2.ParentSkill = strParentSkill;
        M_BaoMing2.Resources = strResources;
        M_BaoMing2.Status = 0;
        M_BaoMing2.Notes = "";
        M_BaoMing2.CheckInfos = "";
        M_BaoMing2.UserId = iUserId;
        M_BaoMing2.AddTime = DateTime.Now;

        int iResult = 0;
        iResult = DalEx.BaoMing2Ex.Insert(M_BaoMing2, ref strResult);
        if (iResult > 0)
        {
            UserType = 1;
            UserId = iUserId;
            UserName = strLoginName;
            MessageBox(strResult, "MyYiGongBaoMing.aspx");
        }
        else
        {
            MessageBox(strResult);
        }
    }
    private void setMeta(string title, string key, string des)
    {
        string strWebTitle = string.Empty, strWebKeywords = string.Empty, strWebDescription = string.Empty;
        Model.DbSystem dbSystem = Dal.DbSystemDal.Select(1);
        if (dbSystem != null)
        {
            strWebTitle = dbSystem.STitle;
            strWebKeywords = dbSystem.SKeywords;
            strWebDescription = dbSystem.SDescription;
        }
        HtmlMeta keywords = new HtmlMeta(); // keywords
        HtmlMeta description = new HtmlMeta(); // description
        keywords.Name = "keywords";
        keywords.Content = string.IsNullOrEmpty(key) ? strWebKeywords : key;

        this.Title = string.IsNullOrEmpty(title) ? strWebTitle : title;

        description.Name = "description";
        description.Content = string.IsNullOrEmpty(des) ? strWebDescription : des;

        Page.Header.Controls.Add(keywords);
        Page.Header.Controls.Add(description);
    }
}
